package com.decompiler.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 窗口大小调整组件
 * 允许用户通过拖动窗口边缘来调整窗口大小
 */
public class WindowResizer {
    private JFrame frame;
    private int resizeBorderWidth = 5;
    private Cursor defaultCursor = Cursor.getDefaultCursor();
    private boolean isResizing = false;
    private Point startPoint;
    private Dimension startSize;
    private int resizeDirection = -1;
    
    // 调整方向常量
    private static final int RESIZE_NONE = -1;
    private static final int RESIZE_NORTH = 0;
    private static final int RESIZE_EAST = 1;
    private static final int RESIZE_SOUTH = 2;
    private static final int RESIZE_WEST = 3;
    private static final int RESIZE_NORTH_EAST = 4;
    private static final int RESIZE_SOUTH_EAST = 5;
    private static final int RESIZE_SOUTH_WEST = 6;
    private static final int RESIZE_NORTH_WEST = 7;
    
    /**
     * 创建窗口大小调整组件
     * 
     * @param frame 要调整大小的窗口
     */
    public WindowResizer(JFrame frame) {
        this.frame = frame;
        setupResizeListeners();
    }
    
    /**
     * 设置调整边框宽度
     * 
     * @param width 边框宽度（像素）
     */
    public void setResizeBorderWidth(int width) {
        this.resizeBorderWidth = width;
    }
    
    /**
     * 设置调整大小的监听器
     */
    private void setupResizeListeners() {
        // 鼠标移动监听器，用于改变鼠标光标
        frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                if (!isResizing) {
                    int direction = getResizeDirection(e.getPoint());
                    updateCursor(direction);
                }
            }
            
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isResizing) {
                    resizeFrame(e.getPoint());
                }
            }
        });
        
        // 鼠标按下和释放监听器，用于开始和结束调整大小
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int direction = getResizeDirection(e.getPoint());
                if (direction != RESIZE_NONE) {
                    isResizing = true;
                    resizeDirection = direction;
                    startPoint = e.getPoint();
                    startSize = frame.getSize();
                }
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                isResizing = false;
                resizeDirection = RESIZE_NONE;
            }
            
            @Override
            public void mouseExited(MouseEvent e) {
                if (!isResizing) {
                    frame.setCursor(defaultCursor);
                }
            }
        });
    }
    
    /**
     * 获取调整方向
     * 
     * @param point 鼠标位置
     * @return 调整方向常量
     */
    private int getResizeDirection(Point point) {
        int x = point.x;
        int y = point.y;
        int width = frame.getWidth();
        int height = frame.getHeight();
        
        boolean north = y <= resizeBorderWidth;
        boolean south = y >= height - resizeBorderWidth;
        boolean east = x >= width - resizeBorderWidth;
        boolean west = x <= resizeBorderWidth;
        
        if (north && east) return RESIZE_NORTH_EAST;
        if (south && east) return RESIZE_SOUTH_EAST;
        if (south && west) return RESIZE_SOUTH_WEST;
        if (north && west) return RESIZE_NORTH_WEST;
        if (north) return RESIZE_NORTH;
        if (east) return RESIZE_EAST;
        if (south) return RESIZE_SOUTH;
        if (west) return RESIZE_WEST;
        
        return RESIZE_NONE;
    }
    
    /**
     * 更新鼠标光标
     * 
     * @param direction 调整方向
     */
    private void updateCursor(int direction) {
        switch (direction) {
            case RESIZE_NORTH:
            case RESIZE_SOUTH:
                frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                break;
            case RESIZE_EAST:
            case RESIZE_WEST:
                frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                break;
            case RESIZE_NORTH_EAST:
            case RESIZE_SOUTH_WEST:
                frame.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
                break;
            case RESIZE_NORTH_WEST:
            case RESIZE_SOUTH_EAST:
                frame.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
                break;
            default:
                frame.setCursor(defaultCursor);
                break;
        }
    }
    
    /**
     * 调整窗口大小
     * 
     * @param currentPoint 当前鼠标位置
     */
    private void resizeFrame(Point currentPoint) {
        int dx = currentPoint.x - startPoint.x;
        int dy = currentPoint.y - startPoint.y;
        int newWidth = startSize.width;
        int newHeight = startSize.height;
        int newX = frame.getX();
        int newY = frame.getY();
        
        // 根据调整方向计算新的窗口大小和位置
        switch (resizeDirection) {
            case RESIZE_NORTH:
                newHeight = startSize.height - dy;
                newY = frame.getY() + dy;
                break;
            case RESIZE_EAST:
                newWidth = startSize.width + dx;
                break;
            case RESIZE_SOUTH:
                newHeight = startSize.height + dy;
                break;
            case RESIZE_WEST:
                newWidth = startSize.width - dx;
                newX = frame.getX() + dx;
                break;
            case RESIZE_NORTH_EAST:
                newWidth = startSize.width + dx;
                newHeight = startSize.height - dy;
                newY = frame.getY() + dy;
                break;
            case RESIZE_SOUTH_EAST:
                newWidth = startSize.width + dx;
                newHeight = startSize.height + dy;
                break;
            case RESIZE_SOUTH_WEST:
                newWidth = startSize.width - dx;
                newHeight = startSize.height + dy;
                newX = frame.getX() + dx;
                break;
            case RESIZE_NORTH_WEST:
                newWidth = startSize.width - dx;
                newHeight = startSize.height - dy;
                newX = frame.getX() + dx;
                newY = frame.getY() + dy;
                break;
        }
        
        // 设置最小窗口大小
        newWidth = Math.max(newWidth, 400);
        newHeight = Math.max(newHeight, 300);
        
        // 应用新的窗口大小和位置
        frame.setBounds(newX, newY, newWidth, newHeight);
        frame.revalidate();
    }
}